package com.sinven.action;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.sinven.entity.User;
import com.sinven.service.UserService;
import com.sinven.util.GetUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Controller
@Scope("prototype")
@ParentPackage("json-default")
public class UserAction extends ActionSupport {

    @Autowired
    @Qualifier("UserService")
    private UserService userService;


    private User users;

    public User getUsers() {
        return users;
    }

    public void setUsers(User users) {
        this.users = users;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @Action(value = "getUser", results = {@Result(type = "json")})//返回的数据格式为json
    public String getUser() throws IOException {
        PrintWriter out = GetUtil.getOut();
        List<User> userList = userService.findUser();
        String jsonString = JSON.toJSONString(userList);
        out.print(jsonString);


        return NONE;
    }


    /**
     * 用户登录
     *
     * @return 返回用户所有信息
     * @throws IOException
     */
    @Action(value = "findUserByConde", results = {@Result(type = "json")})//返回的数据格式为json
    public String findUserByConde() throws IOException {
        PrintWriter out = GetUtil.getOut();
        User user = userService.findUserByCode(users.getUsername(), users.getPassword());
        if (user != null) {
            uplogintime(user.getUsername());
            String jsonString = JSON.toJSONString(user);
            out.print(jsonString);

        }
        return NONE;
    }

    /**
     * 用户注册
     *
     * @return 返回用户数据库中的主键
     * @throws IOException
     */
    @Action(value = "RegisteredUser", results = {@Result(type = "json")})//返回的数据格式为json
    public String RegisteredUser() throws IOException {
        PrintWriter out = GetUtil.getOut();
        User user = users;
        Integer integer = userService.RegisteredUser(user);
        if (integer > -1) {
            out.print("ok");
        }
        return NONE;
    }

    /**
     * 最后登录时间OK 还需要微调
     *
     * @return
     * @throws IOException
     */
    //@Action(value = "uplogintime",results = {@Result(type = "json")})//返回的数据格式为json
    public String uplogintime(String username) throws IOException {
        PrintWriter out = GetUtil.getOut();
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        int result = userService.upLoginTime(timestamp, users.getUsername());
        if (result > 0) {

            out.print("ok");
        }
        return NONE;
    }
}



